package com.link.base.base.applyqual.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.link.base.base.applyqual.model.ApplyQual;
import com.link.base.base.applyqual.model.ApplyQualInfo;
import com.link.base.base.applyqual.service.ApplyQualService;
import com.link.base.base.approval.model.Flow;
import com.link.base.base.approval.service.FlowService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liushoulong
 * @date 2018-11-8
 * @desc 业务申请：运营资质文件申请
 * @desc 厂房资质文件申请
 */
@Controller("ApplyQualController")
@RequestMapping("/link/applyQual")
public class ApplyQualController extends BasicController<ApplyQual> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private ApplyQualService applyQualService;

    @Resource
    private FlowService flowService;

    @Override
    public BasicService<ApplyQual> getBasicService() throws Exception {
        return applyQualService;
    }

    /**
     * 提交申请，创建审批流程
     */
    @RequestMapping(value = "/submit")
    @ResponseBody
    public Map<String, Object> submit(@RequestBody ApplyQualInfo applyQualInfo,
                                      HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(8);
        ObjectMapper mapper = new ObjectMapper();
        try {
            String approvalDetail = mapper.writeValueAsString(applyQualInfo);
            if (null == applyQualInfo.getApplyQual().getId()) {
                throw new ServiceException("PUBLIC-007");
            }
            applyQualService.accntSigningSubmit(applyQualInfo.getApplyQual(), approvalDetail);
            ApplyQual newTra = applyQualService.queryById(applyQualInfo.getApplyQual());
            result.put("newRow", newTra);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 审批处理 第一个节点填写数据 审批详情数据修改
     *
     * @param apptrovalInfo
     * @param session
     * @return
     */
    @RequestMapping(value = "/approvalDetailUpdate", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> approvalDetailUpdate(@RequestBody ApplyQualInfo apptrovalInfo, HttpSession session) {
        Map<String, Object> result = new HashMap<String, Object>(8);
        ObjectMapper mapper = new ObjectMapper();
        try {
            if (null == apptrovalInfo.getApprovalId()) {
                throw new ServiceException("APPLY-001");
            }
            Flow flow = new Flow();
            flow.setId(apptrovalInfo.getApprovalId());
            Flow flowObj = flowService.queryById(flow);
            if (flowObj == null) {
                throw new ServiceException("APPLY-002");
            }
            /*审批详情封装json字符串*/
            String approvalDetail = mapper.writeValueAsString(apptrovalInfo);
            /**审批流程更新**/
            flowObj.setFlowObjDetail(approvalDetail);
            flowService.update(flowObj);

            result.put("success", true);
            result.put("newRow", flowService.queryById(flowObj));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 审核处理 最后一个节点审核成功 修改审批对象
     *
     * @param approvalInfo
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/approvalObjectUpdate", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> approvalObjectUpdate(@RequestBody ApplyQualInfo approvalInfo, HttpSession session,
                                                    HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(8);
        ObjectMapper mapper = new ObjectMapper();
        try {
            applyQualService.approvalObjectUpdate(approvalInfo);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
